package com.ybkj.daijia.api.beatpay.utils;

import com.ybkj.daijia.serverUtils.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 翼支付创建订单类
 *
 * @author zane
 */
public class BestPayCreateOrder {

    private static Logger logger = LoggerFactory.getLogger(BestPayCreateOrder.class);


    /**
     * 翼支付APP下单
     *
     * @param url        下单接口地址
     * @param orderModel 订单模型
     * @return 下单结果
     * @throws Exception
     */
    public static boolean createByApp(String url, BestPayOrderModel orderModel) throws Exception {
        // 进行MAC加密
        StringBuffer macBuffer = new StringBuffer();
        macBuffer
            .append("MERCHANTID=").append(orderModel.getMERCHANTID())
            .append("&ORDERSEQ=").append(orderModel.getORDERSEQ())
            .append("&ORDERREQTRANSEQ=").append(orderModel.getORDERREQTRANSEQ())
            .append("&ORDERREQTIME=").append(orderModel.getORDERREQTIME())
            .append("&RISKCONTROLINFO=").append(orderModel.getRISKCONTROLINFO())
            .append("&KEY=").append(orderModel.getKEY());

        logger.info("翼支付下单加密串：" + macBuffer.toString());

        // 加密
        String mac = CryptTool.md5Digest(macBuffer.toString());
        logger.info("翼支付下单加密Mac：" + mac);

        // 请求串
        StringBuffer orderBuffer = new StringBuffer();
        orderBuffer
            .append("MERCHANTID=").append(orderModel.getMERCHANTID())
            .append("&SUBMERCHANTID=").append(orderModel.getSUBMERCHANTID())
            .append("&ORDERSEQ=").append(orderModel.getORDERSEQ())
            .append("&ORDERREQTRANSEQ=").append(orderModel.getORDERREQTRANSEQ())
            .append("&ORDERREQTIME=").append(orderModel.getORDERREQTIME())
            .append("&TRANSCODE=").append(orderModel.getTRANSCODE())
            .append("&ORDERAMT=").append(orderModel.getORDERAMT())
            .append("&ORDERCCY=").append(orderModel.getORDERCCY())
            .append("&SERVICECODE=").append(orderModel.getSERVICECODE())
            .append("&PRODUCTID=").append(orderModel.getPRODUCTID())
            .append("&PRODUCTDESC=").append(orderModel.getPRODUCTDESC())
            .append("&LOGINNO=").append(orderModel.getLOGINNO())
            .append("&PROVINCECODE=").append(orderModel.getPROVINCECODE())
            .append("&CITYCODE=").append(orderModel.getCITYCODE())
            .append("&DIVDETAILS=").append(orderModel.getDIVDETAILS())
            .append("&ENCODETYPE=").append(orderModel.getENCODETYPE())
            .append("&MAC=").append(mac)
            .append("&SESSIONKEY=").append(orderModel.getSESSIONKEY())
            .append("&ENCODE=").append(orderModel.getENCODE())
            .append("&ATTACH=").append(orderModel.getATTACH())
            .append("&RISKCONTROLINFO=").append(orderModel.getRISKCONTROLINFO());

        String reqBody = orderBuffer.toString();
        logger.info("翼支付下单请求串：" + reqBody);

        // 下单
        String resp = HttpClientUtil.PostResponseDataByID(url, reqBody);
        logger.info("翼支付下单响应数据：" + resp);

        // 下单请求失败
        if (null == resp
            || resp.isEmpty()
            || !resp.contains("&")) {
            return false;
        }

        // 请求成功，获取返回数据(返回的是"code&msg"的这种格式，我们要来解析他)
        String[] result = resp.split("&");
        String code = result[0];
        String codeMsg = result[1];

        // 看是否下单成功
        if (!code.equals("00")) {

            return false;
        }

        return true;
    }

    public static void main(String[] args) throws Exception {
//		String mch_id = "02650108010471422";
//		String key = "C86DC25255320814CE1E07D9550A77FA1C8C5D69A62ED409";
//		String orderTime = "20170615104856";
//		String orderId = "137432137436";
//
//		BestPayOrderModel model = new BestPayOrderModel();
//		model.setATTACH("null");
//		model.setCITYCODE("");
//		model.setDIVDETAILS("");
//		model.setENCODE("");
//		model.setENCODETYPE("1");
//		model.setKEY(key);
//		model.setLOGINNO("");
//		model.setMERCHANTID(mch_id);
//		model.setORDERAMT("100"); // 分
//		model.setORDERCCY("RMB");
//		model.setORDERREQTIME(orderTime);
//		model.setORDERREQTRANSEQ(orderId);
//		model.setORDERSEQ(orderId);
//		model.setPRODUCTDESC("测试商品描述");
//		model.setPRODUCTID("04");
//		model.setPROVINCECODE("");
//		model.setRISKCONTROLINFO("{test:001}");
//		model.setSERVICECODE("05");
//		model.setSESSIONKEY("");
//		model.setSUBMERCHANTID("");
//		model.setTRANSCODE("01");
//		
//		boolean rs = createByApp("https://webpaywg.bestpay.com.cn/order.action", model);
//		System.out.println(rs);
    }
}
